"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:ProjectBcxyCtrl
 * @description
 * # ProjectBcxyCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("ProjectBcxyCtrl", function(
    $scope,
    $location,
    openApi,
    Util,
    $routeParams,
    FormSubmit,
    localStorageService
  ) {
    var id = ($scope.process_id = $routeParams.id);
    $scope.ifchange=[{key:"变更",value:1},{key:"不变更",value:0}];

    $scope.chooseProject = function() {
      // 将列表的数据序列化存入缓存以便回来时候使用
      Util.savaParamForReview($scope.param, id);
      $location.path("project/chooseproject");
    };

    $scope.chooseContract = function() {
      var params = $scope.param;
      if (!params.projectId || !params.contractType) {
        Util.Toast("请先选择项目与合同类型");
        return;
      }
      var projectParams = {
        formKey: params.contractType,
        fieldName: "contractNo",
        projectId: params.projectId,
        projectNo:params.projectNo
      };

      // 清掉关联的编号
      Util.savaParamForReview(params, id);
      localStorageService.set("projectParams", projectParams);
      $location.path("/project/choosedata");
    };

    $scope.submit = function(operationId) {
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.supplementcontract;
      options.param = angular.copy($scope.param, {});
      FormSubmit.submit(options,$scope);
    };
    $scope.validate = function() {
      if (!$scope.param.projectName) {
        Util.Toast("请选择项目");
        return;
      }
      if (!$scope.param.contractType) {
        Util.Toast("请选择合同类型");
        return;
      }
      if (
        !$scope.param.amountSupplement &&
        $scope.param.amountSupplement!==0
      ) {
        Util.Toast("请选择是否变更合同金额");
        return;
      }
      if (
        $scope.param.amountSupplement == 1 &&
        !$scope.param.supplementAmount
      ) {
        Util.Toast("请输入需变更的金额");
        return;
      }
      if (!$scope.param.supplementContent) {
        Util.Toast("请输入变更内容");
        return;
      }

      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    };
    var init = function() {
      // 获取回显的详细信息
      Util.getDetailData(id, function(data) {
        $scope.param = data;
        var data = localStorageService.get("projectinfo");
        localStorageService.remove("projectinfo");
        // 获取项目关联数据
        var projectreldata = localStorageService.get("projectreldata");
        localStorageService.remove("projectreldata");
        $scope.param = Object.assign({}, $scope.param, data, projectreldata);
      });
    };
    // 初始化
    init();
  });
